﻿<?php
		if(isset($_GET["action"]) && "save" == $_GET["action"]){
			for($i=1;$i<100;$i++){
				if(!isset($_POST["title".$i])){
					break;
				}
				
				$title = $_POST["title".$i];
				$content = $_POST["content".$i];				
				if(isset($_POST["id".$i])){
					$id = $_POST["id".$i];
					DbUtils::execute("UPDATE `template_common2` SET `content` = '".$content."', `title` = '".$title."' WHERE `id` = ".$id);
				}else{
					DbUtils::execute("INSERT INTO `template_common2` (`id`, `title`, `content`, `pageId`, `order`) VALUES ('', '".$title."', '".$content."', '".$pageId."', '0')");
				}
			}
			header('Location: index.php?type=content&pageId='.$pageId);	
		}
?>		

<div>
<form name="form" method="post" action="index.php?type=content&pageId=<?php echo($pageId) ?>&action=save">
	<table id="formTable">
		<?php
			$result = DbUtils::execute("select * from `template_common2` where pageId='".$pageId."'");
			while($row = mysql_fetch_array($result)){
				?>
				<tr><td>
					<input type="hidden" name="id<?php echo($row["id"]) ?>" value="<?php echo($row["id"]) ?>">
					<span class="number"><?php echo($row["id"]) ?></span>:<br/>
					标题：<input type="text" name="title<?php echo($row["id"]) ?>" value="<?php echo($row["title"]) ?>"/><br/>
					内容：<textarea name="content<?php echo($row["id"]) ?>" cols="80" rows="15"><?php echo($row["content"]) ?></textarea>
				</td></tr>
		<?php		
			}
		?>
	</table>
	<input type="button" id="addButton" value="新增"/>&nbsp;
	<input type="submit" value="保存"/>
</form>
</div>
<script>
$("#addButton").click(function(){
	var idStr = $(".number").last().html();
	var id = !idStr ? 1 : parseInt(idStr) + 1;
	$("#formTable").append("<tr><td>"
	+ '<span class="number">' + id + '</span>:<br/>'
	+ '标题：<input type="text" name="title' + id + '" value=""/><br/>'
	+ '内容：<textarea name="content' + id + '" cols="80" rows="15"></textarea>'
	+ '</td></tr>');
});
</script>